home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks '96 / MenuMadness / Source / C Source / QDUtils.c < prev    next >
Encoding:
Text File  |  1996-06-21  |  4.1 KB  |  255 lines  |  [TEXT/CWIE]

  1.  
  2. // Maf Vosburgh 
  3.  
  4.  
  5.  
  6. #include    <Icons.h>
  7. #include    <QDOffscreen.h>
  8. #include    <ImageCompression.h>
  9. #include    <string.h>
  10. #include    <Palettes.h>
  11. #include     "QDUtils.h"
  12.  
  13.  
  14.  
  15.  
  16. OSErr PlotSmallIcon(Point    p, short iconID)
  17. {
  18.     Rect    r;
  19.     
  20.     SetRect(&r, p.h -8, p.v -8, p.h + 8, p.v + 8);
  21.     return PlotIconID(&r, atNone, ttNone, iconID);
  22. }
  23.  
  24.  
  25. OSErr PlotSmallMenuIcon(Point    p, short iconID, short transform)
  26. {
  27.     Rect    r;
  28.     
  29.     SetRect(&r, p.h -8, p.v -8, p.h + 8, p.v + 8);
  30.     return PlotIconID(&r, atNone, transform, iconID);
  31. }
  32.  
  33.  
  34. void    OffsetPt(Point *p, short dh, short dv)
  35. {
  36.     p->h += dh;
  37.     p->v += dv;
  38. }
  39.  
  40.  
  41. short GetGDDepth(GDHandle    gd)
  42. {
  43.     return gd[0]->gdPMap[0]->pixelSize;
  44. }
  45.  
  46.  
  47. short GetGDMode(GDHandle    gd)
  48. {
  49.     return gd[0]->gdFlags & 1;
  50. }
  51.  
  52. short    MaxDepth(GDHandle    gd)
  53. {
  54.     short x;
  55.     for (x = 32 ; x ; x >>=1)
  56.         if (HasDepth(gd, x, 0, 0))
  57.             return x;
  58.             
  59.     return 0;// will never get here
  60. }
  61.  
  62.  
  63. void MafInsetRect(Rect *r, short hInset, short vInset)
  64. {
  65.     r->left += hInset;
  66.     r->right -= hInset;
  67.     r->top += vInset;
  68.     r->bottom -= vInset;
  69. }
  70.  
  71. void MafOffsetRect(Rect *r, short hOffset, short vOffset)
  72. {
  73.     r->left += hOffset;
  74.     r->right += hOffset;
  75.     r->top += vOffset;
  76.     r->bottom += vOffset;
  77. }
  78.  
  79. Boolean MafPtInRect(Point    p, Rect *r)
  80. {
  81.     return ((p.h >= r->left) && (p.h <= r->right) && (p.v >= r->top) && (p.v <= r->bottom)); 
  82. }
  83.  
  84. void MafSetRect(Rect    *r, short left, short top, short right, short bottom)
  85. {
  86.     r->top = top;
  87.     r->left = left;
  88.     r->right = right;
  89.     r->bottom = bottom;
  90.     
  91. }
  92.  
  93. void ZeroRect(Rect*    r)
  94. {
  95.     MafOffsetRect(r, -r->left, -r->top);
  96. }
  97.  
  98. void CentreRect(Rect    *subRect, Rect masterRect)
  99. {
  100.     ZeroRect(subRect);
  101.     MafOffsetRect(subRect,  ((masterRect.left + masterRect.right) >>1) - (subRect->right >>1),
  102.                             ((masterRect.top + masterRect.bottom) >>1) - (subRect->bottom >>1));
  103. }
  104.  
  105.  
  106.  
  107. void HiliteRect(Rect    *r)
  108. {
  109. // clear high bit of HiliteMode lo mem global
  110. #ifdef THINK_C
  111.     BitClr ((Ptr)0x938, pHiliteBit); // pHiliteBit = 0, as BitClr goes from high to low
  112. #else
  113.     LMSetHiliteMode( LMGetHiliteMode() & 0x7F ); 
  114. #endif
  115.         
  116.     InvertRect(r);                // this InvertRect now hilites, not inverts
  117. }                                // HiliteMode is reset automatically
  118.                                 // by QuickDraw
  119.  
  120.  
  121.  
  122.  
  123. short    RWidth(Rect    r)
  124. {
  125.     return r.right - r.left;
  126. }
  127.  
  128. short    RHeight(Rect    r)
  129. {
  130.     return r.bottom - r.top;
  131. }
  132.  
  133. // little utility proc, like SetRect
  134. void SetRGBColor(RGBColor *rgb, unsigned short r,unsigned short g,unsigned short b)
  135. {
  136.     rgb->red = r;
  137.     rgb->green = g;
  138.     rgb->blue = b;
  139. }
  140.  
  141.  
  142. void SetRGBBackColor(unsigned short r,unsigned short g,unsigned short b)
  143. {
  144.     RGBColor    rgb;
  145.     
  146.     SetRGBColor(&rgb, r,g,b);
  147.     
  148.     RGBBackColor(&rgb);
  149. }
  150.  
  151.  
  152.  
  153. void SetRGBForeColor(unsigned short r,unsigned short g,unsigned short b)
  154. {
  155.     RGBColor    rgb;
  156.     
  157.     SetRGBColor(&rgb, r,g,b);
  158.     
  159.     RGBForeColor(&rgb);
  160. }
  161.  
  162. void QDNormal(void)
  163. {
  164.     ForeColor(blackColor);
  165.     BackColor(whiteColor);
  166.     PenNormal();
  167. }
  168.  
  169.  
  170.  
  171. void LocalToGlobalRect(Rect * r)
  172. {
  173.     LocalToGlobal( (Point*) r);
  174.     LocalToGlobal( (Point*) r + 1);
  175. }
  176.  
  177. void GlobalToLocalRect(Rect * r)
  178. {
  179.     GlobalToLocal( (Point*) r);
  180.     GlobalToLocal( (Point*) r + 1);
  181. }
  182.  
  183.  
  184. Point    GetWindowLoc(WindowPtr    theWindow)
  185. {
  186.     GrafPtr        savePort;
  187.     Point        windowLoc = {0,0};
  188.     
  189.     GetPort(&savePort);
  190.     SetPort(theWindow);
  191.     LocalToGlobal(&windowLoc);
  192.     SetPort(savePort);
  193.     return windowLoc;
  194. }
  195.  
  196.  
  197. short    WindowToDepth(WindowPtr    theWindow)
  198. {
  199.     GrafPtr        savePort;
  200.     GDHandle    gd;
  201.     Rect        windowRect;
  202.     
  203.     GetPort(&savePort);
  204.     SetPort(theWindow);
  205.     windowRect = theWindow->portRect;
  206.     LocalToGlobalRect(&windowRect);
  207.     SetPort(savePort);
  208.     gd = GetMaxDevice(&windowRect);
  209.     if (!gd)
  210.         gd = GetMainDevice();
  211.     return (*(*gd)->gdPMap)->pixelSize;
  212. }
  213.  
  214. GDHandle GetCurrentGD(Point    loc)
  215. {
  216.     GDHandle    gD = GetDeviceList();
  217.     
  218.     do
  219.         {
  220.         if  (PtInRect(loc, &gD[0]->gdRect))
  221.             return gD;
  222.         else
  223.             gD =  (GDHandle) gD[0]->gdNextGD;
  224.         }
  225.     while (gD);
  226.         
  227.     return nil;
  228. }
  229.  
  230.  
  231.  
  232. // example  AlignRect(&movableRect, fixedRect, kAlignTop + kAlignRight);
  233.  
  234. void    AlignRect(Rect    *r1, Rect    r2, short alignCode)
  235. {
  236.     short xOffset = 0;
  237.     short yOffset = 0;
  238.  
  239.     if (alignCode & kAlignRectLeft)
  240.         xOffset = r2.left - r1->left;
  241.     else if (alignCode & kAlignRectRight)
  242.         xOffset = r2.right - r1->right;
  243.         
  244.         
  245.     if (alignCode & kAlignRectTop)
  246.         yOffset = r2.top - r1->top;
  247.     else if (alignCode & kAlignRectBottom)
  248.         yOffset = r2.bottom - r1->bottom;
  249.     
  250.     MafOffsetRect(r1, xOffset, yOffset);
  251. }
  252.  
  253.  
  254.  
  255.